layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    zmnLayUtil.layFormVerify(form);
    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;

        deleteComma(data);

        if (data.priceStandard == 2) {
            if (!verify(data.price)){
                layer.alert("销售价格式错误:应输入正整数或者两位小数,且最大值不超过1000000");
                return false;
            }
        } else {
            if (data.negotiableMinPrice != null && data.negotiableMinPrice != undefined && data.negotiableMinPrice != ''
            && data.negotiableMaxPrice != null && data.negotiableMaxPrice != undefined && data.negotiableMaxPrice != '') {
            } else {
                layer.alert("销售价价格区间必须同时填写");
                return false;
            }

            if (!verify(data.negotiableMinPrice)){
                layer.alert("销售价价格区间左侧格式错误:应输入正整数或者两位小数,且最大值不超过1000000");
                return false;
            }

            if (!verify(data.negotiableMaxPrice)){
                layer.alert("销售价价格区间右侧格式错误:应输入正整数或者两位小数,且最大值不超过1000000");
                return false;
            }

            if(data.negotiableMaxPrice*100 <= data.negotiableMinPrice*100){
                layer.alert("销售价价格区间右侧必须大于左侧");
                return false;
            }
        }


        if (!verify(data.internalSettlementPrice)){
            layer.alert("内部价格式错误:应输入正整数或者两位小数,且最大值不超过1000000");
            return false;
        }

        if (!verify(data.externalSettlementPrice)){
            layer.alert("外部价格式错误:应输入正整数或者两位小数,且最大值不超过1000000");
            return false;
        }

        if (data.price == null || data.price == ''){
            data.price = "";
        } else{
            data.price = data.price*100;
        }

        if (data.internalSettlementPrice == null || data.internalSettlementPrice == ''){
            data.internalSettlementPrice = "";
        } else{
            data.internalSettlementPrice = data.internalSettlementPrice*100;
        }

        if (data.externalSettlementPrice == null || data.externalSettlementPrice == ''){
            data.externalSettlementPrice = "";
        } else{
            data.externalSettlementPrice = data.externalSettlementPrice*100;
        }

        if (data.negotiableMaxPrice == null || data.negotiableMaxPrice == ''){
            data.negotiableMaxPrice = "";
        } else{
            data.negotiableMaxPrice = data.negotiableMaxPrice*100;
        }

        if (data.negotiableMinPrice == null || data.negotiableMinPrice == ''){
            data.negotiableMinPrice = "";
        } else{
            data.negotiableMinPrice = data.negotiableMinPrice*100;
        }

        var url = "/product/tariff/item/city/price/modifyCityPrice.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if(resp.status == 200) {
                    setTimeout(function () {
                        window.location.href = "/product/tariff/item/city/price/list.action?productId=" + json.vo.productId + "&tariffId=" + json.vo.tariffId + "&groupId=" + json.vo.groupId + "&itemId=" + json.vo.itemId + "&priceStandard=" + json.vo.priceStandard;
                    },1000);
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });

    //对外暴露的接口
    exports('serv_product_tariff_item_city_price_detail', {});
});

function verify(testValue) {
    var reg=/^\d+(\.\d{1,2})?$/;

    if (testValue != null && testValue != undefined && testValue != ""){
        if(!reg.test(testValue))
        {
            return false;
        }

        if (testValue == '0' || testValue == '0.0' || testValue == '0.00'){
            return false;
        }

        let value = parseFloat(testValue);
        if (0 < value && value <= 1000000){
        } else{
            return false;
        }
    }

    return true;
}

function deleteComma(data){
    if (data.price != undefined) {
        data.price = data.price.replace(/,/g, "");
    }
    if (data.negotiableMinPrice != undefined) {
        data.negotiableMinPrice = data.negotiableMinPrice.replace(/,/g, "");
    }
    if (data.negotiableMaxPrice != undefined) {
        data.negotiableMaxPrice = data.negotiableMaxPrice.replace(/,/g, "");
    }
    if (data.internalSettlementPrice != undefined) {
        data.internalSettlementPrice = data.internalSettlementPrice.replace(/,/g, "");
    }
    if (data.externalSettlementPrice != undefined) {
        data.externalSettlementPrice = data.externalSettlementPrice.replace(/,/g, "");
    }
}